<template>
  <div class="wrapp">
    <div class="head">
      <div class="title">项目详情</div>
      <span>{{getParams.row.projectName}}</span>
      <Button type="primary" style="float:right" @click="toSetCharacter">角色设置</Button>
    </div>
    <div class="banner">
      <Tabs :value="typeRelate" type="card">
        <TabPane label="关联用户" name="relateUser">
          <!-- <Button type="primary" @click="toAddProjectPage">新增关联用户</Button> -->
          <div class="table">
            <Table :columns="columnsDataUser_1" :data="userShowData" :border="true" v-if="typeProject=='通用'"></Table>
            <Table :columns="columnsDataUser_2" :data="userShowData" :border="true" v-else-if="typeProject=='共建'"></Table>
            <Table :columns="columnsDataUser_3" :data="userShowData" :border="true" v-else-if="typeProject=='节点'"></Table>
            <Table :columns="columnsDataUser_5" :data="userShowData" :border="true" v-else-if="typeProject=='共建2'"></Table>
            <Table :columns="columnsDataUser_4" :data="userShowData" :border="true" v-else></Table>
            <Page :total="pageTotalUser" :current="pageNumUser" :page-size="pageSizeUser" show-total placement="top" @on-change="handlePageUser" style="margin-top:20px;"></Page>
          </div>
        </TabPane>
        <TabPane label="关联矿机" name="relateDevice">
          <Table :columns="columnsDataDevice" :data="deviceShowData" :border="true"></Table>
          <Page :total="pageTotalDevice" :current="pageNumDevice" :page-size="pageSizeDevice" show-total placement="top" @on-change="handlePageDevice" style="margin-top:20px;"></Page>
        </TabPane>
        <TabPane label="分润规则" name="rule" v-if="typeProject=='共建2'">
          <Button type="primary" @click="toAdd">新增合同</Button>
          <Button type="primary" @click="contractModle">合同模板</Button>
          <!-- <Button type="primary" @click="toSet" style="margin-left:10px;">周期结算</Button> -->
          <div style="margin-top:20px;">
            <Input v-model="searchCond" type="text" placeholder="编号/项目/手机号/设备号" style="width:200px;" @keyup.enter.native="keyset($event)" />
            <Button type="primary" style="margin-left:20px;" @click="searchAction">确定</Button>
          </div>

          <Table :columns="columnsDataRule" :data="RuleShowData" :border="true" style="margin-top:10px;">
            <template slot-scope="{ row, index }" slot="action">
              <div>
                <Button style="margin-right: 5px;" @click="handleEdit(row, index)">编辑</Button>
                <Button @click="check(row, index)">查看</Button>
              </div>
            </template>
          </Table>
          <Page :total="pageTotalRule" :current="pageNumRule" :page-size="pageSizeRule" show-total placement="top" @on-change="handlePageRule" style="margin-top:20px;"></Page>
        </TabPane>
      </Tabs>
    </div>
    <!-- //查看分润规则 -->
    <Modal v-model="secondRule" :mask-closable="false" width="1076" cancel-text="">
      <div>合同编号：{{contractShowData.contractNo}}</div>
      <div>算力数：{{contractShowData.powerAmount}}</div>
      <div>过期时间：{{contractShowData.startDate}}~{{contractShowData.endDate}}</div>
      <div class="sureTitle">总分润规则(客户方和平台方)</div>
      <div v-for="(item,index) in firstArray" :key="index">
        <span>收益累计范围：{{item.intervalBegin}}~{{item.intervalEnd}}元</span>
        <span class="sureMl">客户方分润：{{item.percent}}%</span>
      </div>
      <div class="sureTitle">客户方分润规则</div>
      <div v-for="(item,index) in keHuArray" :key="index+100" style="margin-top:10px;">
        <span>收益累计范围：{{item.intervalBegin}}~{{item.intervalEnd}}元时的参与者是：</span>
        <div v-for="(keHuItem,keHuIndex) in item.data" :key="keHuIndex" style="margin-left:20px;">
          <span>参与者：{{keHuItem.userName}}/{{keHuItem.userMobile}}</span>
          <span class="sureMl">分润比例：{{keHuItem.percent}}%</span>
          <span class="sureMl">角色：{{keHuItem.projectRoleName}}</span>
          <span class="sureMl">备注：{{keHuItem.remark}}</span>

        </div>
      </div>
      <div class="sureTitle">平台方分润规则</div>
      <div v-for="(item,index) in pTArray" :key="index+1000" style="margin-top:10px;">
        <span>收益累计范围：{{item.intervalBegin}}~{{item.intervalEnd}}元时的参与者是：</span>
        <div v-for="(ptItem,ptIndex) in item.data" :key="ptIndex" style="margin-left:20px;">
          <span>参与者：{{ptItem.userName}}/{{ptItem.userMobile}}</span>
          <span class="sureMl">分润比例：{{ptItem.percent}}%</span>
          <span class="sureMl">角色：{{ptItem.projectRoleName}}</span>
          <span class="sureMl">备注：{{ptItem.remark}}</span>

        </div>
      </div>
    </Modal>

    <Modal v-model="isNodesViewShow" :mask-closable="false" width="1024">
      <div>已分配的算力：{{powerTotal}}</div>
      <Table :columns="nodesColumnsData" :data="nodesData" :border="true" style="margin-top:20px;height: 60%;">
        <template slot-scope="{ row, index }" slot="action">
          <Button type="error" @click="remove(row, index)">删除</Button>
        </template>
      </Table>
      <Page :total="nodesTotal" :current="curNodePageNum" :page-size="nodePageSize" show-total placement="top" @on-change="showNodesHandlePage" style="margin-top:20px;"></Page>
    </Modal>

  </div>
</template>

<script>
import apis from "../../util/apis.js";
export default {
  name: "ProjectDetail",
  data() {
    return {
      isSub: false,
      bindSetContract: null,
      isSetShow: false,
      firstArray: [],
      keHuArray: [],
      pTArray: [],
      contractShowData: {
        contractNo: "",
        details: [],
        endDate: "",
        id: null,
        powerAmount: null,
        projectId: null,
        startDate: "",
      },
      typeProject: "通用", //项目类型
      pageTotalUser: 0,
      pageNumUser: 1,
      pageSizeUser: 20,
      pageTotalDevice: 0,
      pageNumDevice: 1,
      pageSizeDevice: 20,
      pageTotalRule: 0,
      pageNumRule: 1,
      pageSizeRule: 20,
      //分润规则列表
      columnsDataRule: [{
        title: "合同编号",
        key: "contractNo",
        align: "center"
      },
      {
        title: "状态",
        key: "status",
        align: "center"
      },
      {
        title: "过期时间",
        key: "endDate",
        align: "center"
      },
      {
        title: "算力",
        key: "powerAmount",
        align: "center"
      },
      {
        title: "操作",
        fixed: "right",
        slot: "action",
        align: "center",
        width: 250
      }
      ],
      RuleShowData: [

      ],
      isHTShow: false,
      projectType: 5,
      searchCond: null,
      //二级分润
      name: null,
      issecondLoading: false,
      contractId: null,
      secondRule: false,
      secondTotal: 0,
      secondPageNum: 1,
      secondPageSize: 20,
      secondCoulumns: [{
        title: "名称",
        key: "name",
        align: "center"
      },
      {
        title: "手机号",
        key: "phone",
        align: "center"
      },
      {
        title: "(二级)分润比例",
        key: "secondProportion",
        align: "center"
      },
      {
        title: "备注",
        key: "remarks",
        align: "center"
      }
      ],
      secondShowData: [],
      getParams: {},
      typeRelate: "", //要定位的tab类型
      settingValue: "", //各种类型要设置的值
      expireDay: "", //算力过期时间(天数)
      //通用
      columnsDataUser_1: [{
        title: "用户ID",
        key: "userId",
        align: "center"
      },
      {
        title: "姓名",
        key: "userName",
        align: "center"
      },
      {
        title: "手机号码",
        key: "mobile",
        align: "center"
      },
      {
        title: "收成百分比",
        key: "projectValue",
        align: "center"
      },
      {
        title: "关联时间",
        key: "createdDate",
        align: "center"
      },
      {
        title: "操作",
        fixed: "right",
        align: "center",
        width: 200,
        render: (h, params) => {
          return h("div", [
            h(
              "Button", {
                props: {
                  type: "error",
                  size: "small"
                },
                style: {
                  marginRight: "5px"
                },
                on: {
                  click: () => {
                    this.unbindingUser(params.row);
                  }
                }
              },
              "解除关联"
            ),
            h(
              "Button", {
                props: {
                  type: "primary",
                  size: "small"
                },
                style: {
                  marginRight: "5px"
                },
                on: {
                  click: () => {
                    this.setFenCheng(params.row);
                  }
                }
              },
              "分成比设置"
            )
          ]);
        }
      }
      ],
      //共建
      columnsDataUser_2: [{
        title: "用户ID",
        key: "userId",
        align: "center"
      },
      {
        title: "姓名",
        key: "userName",
        align: "center"
      },
      {
        title: "手机号码",
        key: "mobile",
        align: "center"
      },

      {
        title: "发起人",
        key: "projectOwner",
        align: "center"
      },

      {
        title: "关联时间",
        key: "createdDate",
        align: "center"
      },
      {
        title: "操作",
        fixed: "right",
        align: "center",
        width: 200,
        render: (h, params) => {
          return h("div", [
            h(
              "Button", {
                props: {
                  type: "error",
                  size: "small"
                },
                style: {
                  marginRight: "5px"
                },
                on: {
                  click: () => {
                    this.unbindingUser(params.row);
                  }
                }
              },
              "解除关联"
            ),

            h(
              "Button", {
                props: {
                  type: "primary",
                  size: "small"
                },
                style: {
                  marginRight: "5px"
                },
                on: {
                  click: () => {
                    this.faQi(params.row);
                  }
                }
              },
              "发起人设置"
            )
          ]);
        }
      }
      ],
      //节点
      columnsDataUser_3: [{
        title: "用户ID",
        key: "userId",
        align: "center"
      },
      {
        title: "姓名",
        key: "userName",
        align: "center"
      },
      {
        title: "手机号码",
        key: "mobile",
        align: "center"
      },
      {
        title: "节点数",
        key: "projectValue",
        align: "center",
        render: (h, params) => {
          return h("div", [
            h(
              "span", {
                style: {
                  borderBottom: "1px solid",
                  cursor: "pointer",
                  color: "red"
                },
                on: {
                  click: () => {
                    this.showNodesView(params.row);
                  }
                }
              },
              this.userShowData[params.index].projectValue
            )
          ]);
        }
      },
      {
        title: "关联时间",
        key: "createdDate",
        align: "center"
      },
      {
        title: "操作",
        fixed: "right",
        align: "center",
        width: 200,
        render: (h, params) => {
          return h("div", [
            h(
              "Button", {
                props: {
                  type: "error",
                  size: "small"
                },
                style: {
                  marginRight: "5px"
                },
                on: {
                  click: () => {
                    this.unbindingUser(params.row);
                  }
                }
              },
              "解除关联"
            ),

            h(
              "Button", {
                props: {
                  type: "error",
                  size: "small"
                },
                style: {
                  marginRight: "5px"
                },
                on: {
                  click: () => {
                    this.setNode(params.row);
                  }
                }
              },
              "节点设置"
            )
          ]);
        }
      }
      ],
      //共建2
      columnsDataUser_5: [{
        title: "用户ID",
        key: "userId",
        align: "center"
      },
      {
        title: "姓名",
        key: "userName",
        align: "center"
      },
      {
        title: "手机号码",
        key: "mobile",
        align: "center"
      },
      {
        title: "手机号码",
        key: "mobile",
        align: "center"
      },
      {
        title: "所属角色",
        key: "roles",
        align: "center"
      },
      {
        title: "微信ID",
        key: "weChatId",
        align: "center"
      },
      {
        title: "操作",
        fixed: "right",
        align: "center",
        width: 200,
        render: (h, params) => {
          return h("div", [
            h(
              "Button", {
                props: {
                  type: "error",
                  size: "small"
                },
                style: {
                  marginRight: "5px"
                },
                on: {
                  click: () => {
                    this.unbindingUser(params.row);
                  }
                }
              },
              "解除关联"
            )
          ]);
        }
      }
      ],
      //算力
      columnsDataUser_4: [{
        title: "用户ID",
        key: "userId",
        align: "center"
      },
      {
        title: "姓名",
        key: "userName",
        align: "center"
      },
      {
        title: "手机号码",
        key: "mobile",
        align: "center"
      },
      {
        title: "算力值",
        key: "projectValue",
        align: "center",
        render: (h, params) => {
          return h("div", [
            h(
              "span", {
                style: {
                  borderBottom: "1px solid",
                  cursor: "pointer",
                  color: "red"
                },
                on: {
                  click: () => {
                    this.showCalPowerView(params.row);
                  }
                }
              },
              this.userShowData[params.index].projectValue
            )
          ]);
        }
      },
      {
        title: "关联时间",
        key: "createdDate",
        align: "center"
      },
      {
        title: "操作",
        fixed: "right",
        align: "center",
        width: 200,
        render: (h, params) => {
          return h("div", [
            h(
              "Button", {
                props: {
                  type: "error",
                  size: "small"
                },
                style: {
                  marginRight: "5px"
                },
                on: {
                  click: () => {
                    this.unbindingUser(params.row);
                  }
                }
              },
              "解除关联"
            ),

            h(
              "Button", {
                props: {
                  type: "error",
                  size: "small"
                },
                style: {
                  marginRight: "5px"
                },
                on: {
                  click: () => {
                    this.suanli(params.row);
                  }
                }
              },
              "算力设置"
            )
          ]);
        }
      }
      ],
      userShowData: [],

      columnsDataDevice: [{
        title: "设备ID",
        key: "iotfsDevId",
        align: "center"
      },
      {
        title: "矿机编号",
        key: "minerNo"
      },
      {
        title: "TeamViewerID",
        key: "teamViewId",
        align: "center"
      },
      {
        title: "IP地址",
        key: "ipAddress",
        align: "center"
      },
      {
        title: "地理位置",
        key: "address",
        align: "center"
      },
      {
        title: "操作",
        fixed: "right",
        align: "center",
        width: 100,
        render: (h, params) => {
          return h("div", [
            h(
              "Button", {
                props: {
                  type: "error",
                  size: "small"
                },
                on: {
                  click: () => {
                    this.unbindingDevice(params.row);
                  }
                }
              },
              "解除关联"
            )
          ]);
        }
      }
      ],
      deviceShowData: [],

      //节点信息展示
      isNodesViewShow: false,
      nodesColumnsData: [{
        title: "已分配算力(MB)",
        key: "configValue",
        width: 150
      },
      {
        title: "有效期",
        key: "lastTime"
      },
      {
        title: "状态",
        key: "statusName",
        width: 150
      },
      {
        title: "来源",
        key: "sourceName",
        width: 150
      },
      {
        title: "操作",
        width: 150,
        slot: "action"
      }
      ],
      nodesData: [],
      curNodePageNum: 1,
      nodePageSize: 20,
      nodesTotal: 0,
      powerTotal: 0
    };
  },
  methods: {
    toSetCharacter() {
      this.$router.push({
        name: "SetCharacter",
        query: {
          param: this.getParams.row.id,
          getParams: this.getParams,
          props: this.getParams
        }
      });
    },
    ///////////////用户的操作////////////////
    ////////////////////////////////////共建2项目的操作/////////////////
    //解除用户关联
    unbindingUser(params) {
      if (params.projectType == "3") {
        if (this.userShowData.length > 1) {
          if (params.projectOwner == true) {
            this.$Message.error("该用户为发起人，不能解除关联");
            return;
          }
        }
      }
      this.$Modal.confirm({
        title: "确认解除关联",
        content: "<div>用户：" +
          "《" +
          params.userName +
          "》" +
          "正在解除项目《" +
          this.getParams.row.projectName +
          "》的关联</div>",
        onOk: () => {
          let token = this.$localStorage.storageGet("TOKEN");
          if (!token) {
            this.$router.push({
              name: "Login"
            });
            return;
          }

          let tempArr = [];
          tempArr.push(params.userId);

          let payload = {
            groupTypeValue: this.getParams.row.projectType,
            projectGroupId: this.getParams.row.id,
            relationType: "remove",
            users: tempArr
          };

          this.$axios({
            method: "post",
            url: apis.bindingAction,
            headers: {
              "Content-Type": "application/json",
              Authorization: "Bearer " + token
            },
            data: payload
          })
            .then(res => {
              if (res.status == "200") {
                this.$Message.success("解除用户绑定成功");
                this.getRelatedUserData(this.pageNumUser - 1);
              } else {
                this.$Message.error(res.data.detail);
              }
            })
            .catch(err => {
              this.$Message.error(err.response.data.message);
            });
        },
        onCancel: () => {
          this.$Message.info("已取消");
        }
      });
    },
    //设置分成比
    setFenCheng(row) {
      this.$Modal.confirm({
        render: h => {
          return h("div", [
            h("h2", "设置信息"),
            h("Input", {
              props: {
                value: this.settingValue,
                autofocus: true,
                placeholder: "请输入..."
              },
              on: {
                input: val => {
                  this.settingValue = val;
                }
              }
            })
          ]);
        },
        onOk: () => {
          this.projectConfigAction(row);
        }
      });
    },
    //发起人设置
    faQi(row) {
      this.$Modal.confirm({
        title: "提示",
        content: "是否设为发起人?",
        onOk: () => {
          this.projectConfigAction(row);
        }
      });
    },
    //节点设置
    setNode(row) {
      this.$Modal.confirm({
        render: h => {
          return h("div", [
            h("h2", "设置信息"),
            h("Input", {
              props: {
                value: this.settingValue,
                autofocus: true,
                placeholder: "请输入..."
              },
              on: {
                input: val => {
                  this.settingValue = val;
                }
              }
            })
          ]);
        },
        onOk: () => {
          this.projectConfigAction(row);
        }
      });
    },
    //算力设置
    suanli(row) {
      this.$Modal.confirm({
        render: h => {
          return h("div", [
            h("h2", "设置信息"),
            h("Input", {
              props: {
                value: this.settingValue,
                autofocus: true,
                placeholder: "请输入算力值(单位MB)(例如:50)"
              },
              on: {
                input: val => {
                  this.settingValue = val;
                }
              }
            }),
            h("Input", {
              props: {
                value: this.expireDay,
                autofocus: true,
                placeholder: "请设置过期天数(例如:30)"
              },
              on: {
                input: val => {
                  this.expireDay = val;
                }
              }
            })
          ]);
        },
        onOk: () => {
          this.projectConfigAction(row);
        }
      });
    },
    //算力删除
    remove(row, index) {
      this.$Modal.confirm({
        title: "确认删除",
        content: "<div>确认要删除算力？</div>",
        width: 570,
        onOk: () => {
          let token = this.$localStorage.storageGet("TOKEN");
          if (!token) {
            this.$router.push({
              name: "Login"
            });
            return;
          }

          let url = apis.deleteProject + "/" + row.id;

          this.$axios({
            method: "delete",
            url: url,
            headers: {
              Authorization: "Bearer " + token
            }
          })
            .then(res => {
              if (res.status == 200) {
                this.$Message.success("删除算力成功!");
                this.isNodesViewShow = false;
                this.getRelatedUserData(this.pageNumUser - 1);
                ///哈喽
              } else {
                this.$Message.error(res.data.detail);
              }
            })
            .catch(err => {
              this.$Message.error(err.response.data.message);
            });
        },
        onCancel: () => {
          this.$Message.info("已取消");
        }
      });
    },
    projectConfigAction(row) {
      let token = this.$localStorage.storageGet("TOKEN");
      if (!token) {
        this.$router.push({
          name: "Login"
        });
        return;
      }

      let userId = row.userId;
      let projectId = row.projectId;
      let projectType = row.projectType;
      let url = apis.projectConfig + "/" + projectId + "/" + userId;

      if (projectType == 1) {
        //通用
        url = url + "?" + "value=" + this.settingValue;
      }

      if (projectType == 2) {
        //节点
        url = url + "?" + "value=" + this.settingValue;
      }

      if (projectType == 3) {
        //共建
        // url = url + '?' + 'value=' + this.settingValue
      }

      if (projectType == 4) {
        //算力
        url = url + "?" + "expireDay=" + this.expireDay;
        url = url + "&" + "value=" + this.settingValue;
      }

      this.$axios({
        method: "post",
        url: url,
        headers: {
          "Content-Type": "application/json",
          Authorization: "Bearer " + token
        }
      })
        .then(res => {
          if (res.status == "200") {
            this.$Message.success("分成比配置成功");
            this.getRelatedUserData(this.pageNumUser - 1);
          } else {
            this.$Message.error(res.data.detail);
          }
        })
        .catch(err => {
          this.$Message.error(err.response.data.message);
        });
    },
    getRelatedUserData(pageNum) {
      let token = this.$localStorage.storageGet("TOKEN");
      if (!token) {
        this.$router.push({
          name: "Login"
        });
        return;
      }

      let url =
        apis.relatedUser +
        "/" +
        this.getParams.row.id +
        "?" +
        "page=" +
        pageNum +
        "&size=" +
        this.pageSizeUser;

      this.$axios({
        method: "get",
        url: url,
        headers: {
          Authorization: "Bearer " + token
        }
      })
        .then(res => {
          if (res.status == "200") {
            res.data.forEach(element => {
              let str = "";
              if (element.roles.length > 0) {
                for (let i = 0; i < element.roles.length; i++) {
                  if (element.roles[i] === "01") {
                    element.roles[i] = "矿工"
                  } else if (element.roles[i] === "02") {
                    element.roles[i] = "矿场主"
                  }
                  else if (element.roles[i] === "03") {
                    element.roles[i] = "经销商"
                  }
                  else if (element.roles[i] === "04") {
                    element.roles[i] = "介绍经销商"
                  }
                  else if (element.roles[i] === "05") {
                    element.roles[i] = "讲师"
                  }
                  else if (element.roles[i] === "06") {
                    element.roles[i] = "平台"
                  }
                  else if (element.roles[i] === "07") {
                    element.roles[i] = "股东"
                  }
                  else if (element.roles[i] === "08") {
                    element.roles[i] = "员工"
                  }
                  else if (element.roles[i] === "09") {
                    element.roles[i] = "投资人"
                  }
                  str = str + " " + element.roles[i];
                }
                element.roles = str;
              } else {
                element.roles = "";
              }
              this.userShowData = res.data;
            });
            this.pageTotalUser = parseInt(res.headers["x-total-count"]);
          } else {
            this.$Message.error(res.data.detail);
          }
        })
        .catch(err => {
          console.log(err)
          this.$Message.error(err.response.data.message);
        });
    },

    toAddProjectPage() { },
    //改变关联用户页码
    handlePageUser(value) {
      this.pageNum = value;
      this.getRelatedUserData(value - 1);
    },
    //节点信息列表
    showNodesView() {
      // this.isNodesViewShow = true
      // this.getProjectConfigData(row, this.curNodePageNum-1)
    },
    //算力信息列表
    showCalPowerView(row) {
      this.isNodesViewShow = true;
      this.getProjectConfigData(row, this.curNodePageNum - 1);
    },
    //节点信息列表翻页
    showNodesHandlePage(value) {
      this.curNodePageNum = value;
      this.getProjectConfigData(this.curNodePageNum - 1);
    },
    //加载节点、算力数据
    getProjectConfigData(row, pageNum) {
      let token = this.$localStorage.storageGet("TOKEN");
      if (!token) {
        this.$router.push({
          name: "Login"
        });
        return;
      }

      let userId = row.userId;
      let projectId = row.projectId;
      let projectType = row.projectType;
      let url =
        apis.projectConfig +
        "/" +
        projectId +
        "/" +
        userId +
        "?" +
        "size=" +
        this.nodePageSize +
        "&page=" +
        pageNum;

      this.$axios({
        method: "get",
        url: url,
        headers: {
          "Content-Type": "application/json",
          Authorization: "Bearer " + token
        }
      })
        .then(res => {
          if (res.status == "200") {
            this.nodesData = res.data;
            let cur = 0;
            res.data.forEach(element => {
              cur += element.configValue;
              element.lastTime = element.startDate + "至\n" + element.endDate;
            });
            this.powerTotal = cur; //计算总算力值
            this.nodesTotal = parseInt(res.headers["x-total-count"]);
          } else {
            this.$Message.error(res.data.detail);
          }
        })
        .catch(err => {
          this.$Message.error(err.response.data.message);
        });
    },
    closeAction() {
      this.isNodesViewShow = false;
    },

    ///////////////矿机的操作////////////////
    //解除矿机关联
    unbindingDevice(params) {
      this.$Modal.confirm({
        title: "确认解除关联",
        content: "<div>设备：" +
          "《" +
          params.id +
          "》" +
          "正在解除项目《" +
          this.getParams.row.projectName +
          "》的关联</div>",
        onOk: () => {
          let token = this.$localStorage.storageGet("TOKEN");
          if (!token) {
            this.$router.push({
              name: "Login"
            });
            return;
          }

          let tempArr = [];
          tempArr.push(params.id);

          let payload = {
            groupTypeValue: this.getParams.row.projectType,
            projectGroupId: this.getParams.row.id,
            relationType: "remove",
            miners: tempArr
          };

          this.$axios({
            method: "post",
            url: apis.bindingAction,
            headers: {
              "Content-Type": "application/json",
              Authorization: "Bearer " + token
            },
            data: payload
          })
            .then(res => {
              if (res.status == "200") {
                this.$Message.success("解除设备绑定成功");
                this.getRelatedMinerData(this.pageNumDevice - 1);
              } else {
                this.$Message.error(res.data.detail);
              }
            })
            .catch(err => {
              this.$Message.error(err.response.data.message);
            });
        },
        onCancel: () => {
          this.$Message.info("已取消");
        }
      });
    },
    getRelatedMinerData(pageNum) {
      let token = this.$localStorage.storageGet("TOKEN");
      if (!token) {
        this.$router.push({
          name: "Login"
        });
        return;
      }

      let url =
        apis.relatedMiner +
        "/" +
        this.getParams.row.id +
        "?" +
        "page=" +
        pageNum +
        "&size=" +
        this.pageSizeDevice;

      this.$axios({
        method: "get",
        url: url,
        headers: {
          Authorization: "Bearer " + token
        }
      })
        .then(res => {
          if (res.status == "200") {
            this.deviceShowData = res.data;
            if (res.headers["x-total-count"]) {
              this.pageTotalDevice = parseInt(res.headers["x-total-count"]);
            }
          } else {
            this.$Message.error(res.data.detail);
          }
        })
        .catch(err => {
          this.$Message.error(err.response.data.message);
        });
    },
    //改变关联矿机页码
    handlePageDevice(value) {
      this.pageNumDevice = value;
      this.getRelatedMinerData(value - 1);
    },

    //项目类型判断
    projectTypeInfo() {
      switch (this.getParams.row.projectType) {
        case 1:
          this.typeProject = "通用";
          break;

        case 2:
          this.typeProject = "节点";
          break;

        case 3:
          this.typeProject = "共建";
          break;

        case 4:
          this.typeProject = "算力";
          break;

        case 5:
          this.typeProject = "共建2";
          break;

        default:
          break;
      }
    },
    ////////////////////////////////////////////分润规则//////////////////////////////////
    toAdd() {
      this.$router.push({
        name: "AddHT",
        query: {
          param: this.getParams,
        }
      });
    },
    contractModle() {
      this.$router.push({
        name: "Modle",
        query: {
          param: this.getParams
        }
      });
    },
    handlePageRule(value) {
      this.pageNumRule = value;
      this.checkContract(this.require(), this.pageNumRule - 1);
    },
    /////////分润查看////////
    check(row, index) {
      this.secondRule = true;
      this.name = row.name;
      this.contractId = row.contractNo;
      this.checkCOntarctData(row.id);
    },
    checkCOntarctData(param) {
      let token = this.$localStorage.storageGet("TOKEN");
      if (!token) {
        this.$router.push({
          name: "Login"
        });
        return;
      }
      let url = apis.checkTHDetail + "/" + param;
      this.$axios({
        method: "get",
        url: url,
        headers: {
          Authorization: "Bearer " + token
        }
      })
        .then(res => {
          if (res.status == "200") {
            this.contractShowData = res.data;
            let arr = res.data.details;
            let firstArray = [];
            let keHuArray = [];
            let pTArray = [];
            arr.forEach(element => {
              if (element.allotType === 1) {
                firstArray.push(element)
              } else if (element.allotType === 2) {
                keHuArray.push(element)
              } else {
                pTArray.push(element)
              }
            })
            this.firstArray = firstArray;
            this.keHuArray = this.mapLoction(keHuArray);
            this.pTArray = this.mapLoction(pTArray);
            console.log(this.pTArray)
            this.firstArray.forEach(element => {
              element.percent = (parseFloat(element.percent) * 100).toFixed(4)
            });
            this.keHuArray.forEach(element => {
              element.data.forEach(ele => {
                ele.percent = (parseFloat(ele.percent) * 100).toFixed(4)
                if (ele.projectRole === "01") {
                  ele.projectRoleName = "矿工"
                } else if (ele.projectRole === "02") {
                  ele.projectRoleName = "矿场主"
                }
                else if (ele.projectRole === "03") {
                  ele.projectRoleName = "经销商"
                }
                else if (ele.projectRole === "04") {
                  ele.projectRoleName = "介绍经销商"
                }
                else if (ele.projectRole === "05") {
                  ele.projectRoleName = "讲师"
                }
                else if (ele.projectRole === "06") {
                  ele.projectRoleName = "平台"
                }
                else if (ele.projectRole === "07") {
                  ele.projectRoleName = "股东"
                }
                else if (ele.projectRole === "08") {
                  ele.projectRoleName = "员工"
                }
                else if (ele.projectRole === "09") {
                  ele.projectRoleName = "投资人"
                }
              });
            });
            this.pTArray.forEach(element => {
              element.data.forEach(ele => {
                ele.percent = (parseFloat(ele.percent) * 100).toFixed(4)
                if (ele.projectRole === "01") {
                  ele.projectRoleName = "矿工"
                } else if (ele.projectRole === "02") {
                  ele.projectRoleName = "矿场主"
                }
                else if (ele.projectRole === "03") {
                  ele.projectRoleName = "经销商"
                }
                else if (ele.projectRole === "04") {
                  ele.projectRoleName = "介绍经销商"
                }
                else if (ele.projectRole === "05") {
                  ele.projectRoleName = "讲师"
                }
                else if (ele.projectRole === "06") {
                  ele.projectRoleName = "平台"
                }
                else if (ele.projectRole === "07") {
                  ele.projectRoleName = "股东"
                }
                else if (ele.projectRole === "08") {
                  ele.projectRoleName = "员工"
                }
                else if (ele.projectRole === "09") {
                  ele.projectRoleName = "投资人"
                }
              });

            });

          } else {
            this.$Message.error(res.data.detail);
          }
        })
        .catch(err => {
          console.log(err)
          this.$Message.error(err.response.data.message);
        });
    },
    mapLoction(arr) {
      var map = {},
        dest = [];
      for (var i = 0; i < arr.length; i++) {
        var ai = arr[i];
        if (!map[ai.intervalBegin]) {
          dest.push({
            intervalBegin: ai.intervalBegin,
            intervalEnd: ai.intervalEnd,
            data: [ai]
          });
          map[ai.intervalBegin] = ai;
        } else {
          for (var j = 0; j < dest.length; j++) {
            var dj = dest[j];
            if (dj.intervalBegin == ai.intervalBegin) {
              dj.data.push(ai);
              break;
            }
          }
        }
      }
      return dest
    },
    secondHandlePage() { },
    ////////////分润编辑///////
    handleEdit(row, index) {
      this.$router.push({
        name: "FenChengEdit",
        query: {
          param: row,
          type: "edit",
          props: this.getParams
        }
      });
    },
    require() {
      let cond = {};
      if (this.searchCond) {
        cond.keyword = this.searchCond;
      }
      if (this.projectType) {
        cond.groupType = this.projectType;
      } else {
        if (this.projectType == 0) {
          cond.groupType = this.projectType;
        }
      }
      return cond;
    },
    searchAction() {
      this.checkContract(this.require(), this.pageNumRule - 1);
    },
    ////////////查看合同列表///
    checkContract(cond, pageNum) {
      let token = this.$localStorage.storageGet("TOKEN");
      if (!token) {
        this.$router.push({
          name: "Login"
        });
        return;
      }
      let url = apis.checkContractList + "/" + this.getParams.row.id;
      if (!cond) {
        url = url + "?" + "page=" + pageNum + "&size=" + this.pageSizeRule;
      } else {
        url = url + "?" + "page=" + pageNum + "&size=" + this.pageSizeRule;
        if (cond.keyword) {
          url = url + "&keyword=" + cond.keyword;
        }
        if (cond.groupType != 0) {
          url = url + "&groupType=" + cond.groupType;
        }
      }
      this.isHTShow = true;

      this.$axios({
        method: "get",
        url: url,
        headers: {
          Authorization: "Bearer " + token
        }
      })
        .then(res => {
          this.isHTShow = false;
          if (res.status == "200") {
            this.RuleShowData = res.data;
            this.pageTotalRule = parseInt(res.headers["x-total-count"]);
            this.RuleShowData.forEach(ele => {
              if (ele.status === 1) {
                ele.status = "未开始"
              } else if (ele.status === 2) {
                ele.status = "进行中"
              } else {
                ele.status = "已过期"
              }
            })
          } else {
            this.$Message.error(res.data.detail);
          }
        })
        .catch(err => {
          this.$Message.error(err.response.data.message);
        });
    },
    keyset(ev) {
      if (ev.keyCode == 13) {
        let cond = this.require();
        this.checkContract(this.require(), this.pageNumRule - 1);
      }
    },
    toSet() {
      this.isSetShow = true;
    },
    sub() {
      this.isSub = true;
    },
    subSure() {
      this.$Message.info("创建成功了哦");
    },
    cancelSet() {
      this.isSetShow = true;
    }
  },
  created() {
    this.getParams = this.$route.query.param;
  },
  mounted() {
    // this.getParams = this.$route.query.param;
    this.typeRelate = this.$route.query.proName;
    this.projectTypeInfo();
    this.getRelatedMinerData(this.pageNumUser - 1);
    this.getRelatedUserData(this.pageNumDevice - 1);
    this.checkContract(this.require(), this.pageNumRule - 1);
  }
};
</script>

<style scoped>
.ml {
  font-size: 20px;
  margin-right: 20px;
}

.balanceTitle {
  margin-bottom: 20px;
}

.wrapp {
  padding: 0 20px;
}

.head {
  border-bottom: 1px solid rgba(0, 0, 0, 0.2);
  padding: 10px 5px;
}

.head span {
  font-size: 16px;
}

.banner {
  margin-top: 20px;
}

.title {
  color: #666;
  font-size: 24px;
  margin-right: 20px;
  display: inline;
}

.table {
  margin-top: 20px;
}

.ivu-table-cell {
  padding-left: 0px;
  padding-right: 0px;
}

/* 展示节点列表 */
.title {
  color: #666;
  font-size: 24px;
}

.banner {
  margin-top: 20px;
}

.table {
  margin-top: 20px;
}

.relateUserView {
  height: 80%;
}

.relateButton {
  display: flex;
  justify-content: space-around;
  margin-top: 20px;
}

.nodesView {
  position: fixed;
  padding: 20px;
  top: 10%;
  right: 10%;
  left: 10%;
  bottom: 10%;
  overflow: scroll;
  outline: 0;
  -webkit-overflow-scrolling: touch;
  background-color: lightgrey;
  filter: alpha(opacity=100);
  background-color: lightgrey;
  z-index: 999;
}

.relateUserTitl {
  font-size: 20px;
  margin-bottom: 20px;
}

.sureTitle {
  font-size: 18px;
  color: red;
  margin-top: 10px;
}

.sureMl {
  margin-left: 20px;
}
.sub {
  width: 200px;
  margin: 0 auto;
}
</style>